﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace HYCodeBase.WinForm.WinFormRemindSuit
{
	internal class SpecialControlAttributeSeter : IAttributeSeter
	{
		private SpecialControlAttributeSeter()
		{
		}

		internal static SpecialControlAttributeSeter GetSpecialControlPropertySeter()
		{
			if (SpecialControlAttributeSeter.m_singleTag == null)
			{
				SpecialControlAttributeSeter.m_singleTag = new SpecialControlAttributeSeter();
			}
			return SpecialControlAttributeSeter.m_singleTag;
		}

		public bool TrySetOneAttribute(ref System.Windows.Forms.Control inputControl, AttributeInformationBean inputAttribute)
		{
			return this.SetSpecialControlAttribute(ref inputControl, inputAttribute);
		}

		private bool SetSpecialControlAttribute(ref System.Windows.Forms.Control inputControl, AttributeInformationBean inputAttribute)
		{
			List<KeyValuePair<string, List<bool>>> lstNodeValGroups = new List<KeyValuePair<string, List<bool>>>();
			List<string> lstInputString = new List<string>();
			bool result;
			try
			{
				if (inputControl is ListView)
				{
					ListView listView = inputControl as ListView;
					lstNodeValGroups = this.SubStringForBoolAttribute(inputAttribute.StrValueString, false);
					result = this.TrySetListViewAttribute(ref listView, lstNodeValGroups);
				}
				else if (inputControl is TreeView)
				{
					TreeView treeView = inputControl as TreeView;
					lstNodeValGroups = this.SubStringForBoolAttribute(inputAttribute.StrValueString, true);
					result = this.TrySetTreeViewAttribute(ref treeView, lstNodeValGroups);
				}
				else if (inputControl is CheckedListBox)
				{
					CheckedListBox checkedListBox = inputControl as CheckedListBox;
					lstNodeValGroups = this.SubStringForBoolAttribute(inputAttribute.StrValueString, false);
					result = this.TrySetCheckedListBoxAttribute(ref checkedListBox, lstNodeValGroups);
				}
				else if (inputControl is ListBox)
				{
					ListBox listBox = inputControl as ListBox;
					lstInputString = this.SubStringForValueAttribute(inputAttribute.StrValueString);
					result = this.TrySetListBoxAttribute(ref listBox, lstInputString);
				}
				else
				{
					result = true;
				}
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}

		private bool TrySetListViewAttribute(ref ListView inputlistView, List<KeyValuePair<string, List<bool>>> lstNodeValGroups)
		{
			bool result;
			try
			{
				for (int i = 0; i < inputlistView.Items.Count; i++)
				{
					inputlistView.Items[i].Checked = lstNodeValGroups[i].Value[0];
				}
				result = true;
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}

		private bool TrySetCheckedListBoxAttribute(ref CheckedListBox inputCheckedListBox, List<KeyValuePair<string, List<bool>>> lstNodeValGroups)
		{
			bool result;
			try
			{
				for (int i = 0; i < inputCheckedListBox.Items.Count; i++)
				{
					inputCheckedListBox.SetItemChecked(i, lstNodeValGroups[i].Value[0]);
				}
				result = true;
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}

		private bool TrySetListBoxAttribute(ref ListBox inputListBox, List<string> lstInputString)
		{
			bool result;
			try
			{
				foreach (string item in lstInputString)
				{
					inputListBox.Items.Add(item);
				}
				result = true;
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}

		private bool TrySetTreeViewAttribute(ref TreeView inputTreeView, List<KeyValuePair<string, List<bool>>> lstNodeValGroups)
		{
			bool result;
			try
			{
				for (int i = 0; i < inputTreeView.Nodes.Count; i++)
				{
					if (lstNodeValGroups[i].Key == SpecialControlAttributeSeter.m_strParentNodeCheckedTag)
					{
						inputTreeView.Nodes[i].Checked = true;
					}
					else
					{
						inputTreeView.Nodes[i].Checked = false;
						for (int j = 0; j < inputTreeView.Nodes[i].Nodes.Count; j++)
						{
							inputTreeView.Nodes[i].Nodes[j].Checked = lstNodeValGroups[i].Value[j];
						}
					}
				}
				result = true;
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}

		private List<KeyValuePair<string, List<bool>>> SubStringForBoolAttribute(string inputStr, bool bIsHaveChildNode)
		{
			List<KeyValuePair<string, List<bool>>> list = new List<KeyValuePair<string, List<bool>>>();
			List<string> list2 = new List<string>();
			List<bool> list3 = new List<bool>();
			string key = string.Empty;
			foreach (string item in inputStr.Split(new char[]
			{
				'-',
				','
			}))
			{
				list2.Add(item);
			}
			foreach (string text in list2)
			{
				if (list3.Count != 0 && (text == SpecialControlAttributeSeter.m_strParentNodeCheckedTag || text == SpecialControlAttributeSeter.m_strParentNodeUnCheckedTag || text == list2.Last<string>()))
				{
					List<bool> list4 = new List<bool>();
					list4.AddRange(list3);
					KeyValuePair<string, List<bool>> item2 = new KeyValuePair<string, List<bool>>(key, list4);
					list.Add(item2);
					list3.Clear();
				}
				if (text == SpecialControlAttributeSeter.m_strParentNodeCheckedTag)
				{
					key = text;
					list3.Add(true);
				}
				else if (text == SpecialControlAttributeSeter.m_strParentNodeUnCheckedTag)
				{
					key = text;
					if (!bIsHaveChildNode)
					{
						list3.Add(false);
					}
				}
				else if (text == SpecialControlAttributeSeter.m_strChildNodeCheckedTag)
				{
					list3.Add(true);
				}
				else if (text == SpecialControlAttributeSeter.m_strChildNodeUnCheckedTag)
				{
					list3.Add(false);
				}
			}
			return list;
		}

		private List<string> SubStringForValueAttribute(string inputStr)
		{
			List<string> list = new List<string>();
			foreach (string text in inputStr.Split(new char[]
			{
				','
			}))
			{
				if (text != "")
				{
					list.Add(text);
				}
			}
			return list;
		}

		private static SpecialControlAttributeSeter m_singleTag = null;

		private static string m_strParentNodeCheckedTag = "ParentNodeChecked";

		private static string m_strParentNodeUnCheckedTag = "ParentNodeUnChecked";

		private static string m_strChildNodeCheckedTag = "ChildNodeChecked";

		private static string m_strChildNodeUnCheckedTag = "ChildNodeUnChecked";
	}
}
